﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel;
using Krile.Kernel.Plugin;
using Krile.Kernel.Plugin.Operations;

namespace Highkick.HOperation
{
    public class HOperation : Operation, IAgent
    {
        Plugin parent;
        public HOperation(Plugin parent)
        {
            this.parent = parent;
            hService = new Highkick.Data.HService();
            hStatusCtl = new HStatusControl(this);
            Bridge.ControlManager.RegistInputMethod(new Dialogs.HInput());
            Bridge.StatusManager.RegistNewSearchData(typeof(Common.Searchs.HatenaHikeSearch));
            Bridge.StatusManager.RegistNewSearchData(typeof(Common.Searchs.KeywordSearch));
            Bridge.KeyAssign.AddAssignCandidateMethod(new Krile.Kernel.Data.Common.KeyAssignCallback(FavCallback), "選択中のステータスがはてなハイクのステータスであれば、スターを追加します。");
        }

        public void FavCallback(Krile.Kernel.Bridges.IForm form, Krile.Kernel.Data.Status s, System.Windows.Forms.Keys k)
        {
            if (s is Data.HStatus)
                HStatusControl.SetFavorite(s.UID, true);
        }

        HStatusControl hStatusCtl;
        Data.HService hService;
        public override string ParentPluginId
        {
            get { return Define.PluginId; }
        }

        public override Operation.OperationTypes OperationType
        {
            get { return OperationTypes.Agent; }
        }

        public override void Begin()
        {
            Core.Init(parent);
            if (Core.Config.Account == null)
            {
                using (var a = new Dialogs.Account())
                {
                    a.ShowDialog();
                }
            }
            if (Core.Config.Account == null)
            {
                Bridge.PluginManager.InactivatePlugin(ParentPlugin);
            }
            else
            {
                HStatusControl.BeginReceive();
            }
        }

        public override void Exit()
        {
            Core.Config.SaveConfig();
        }

        public override Dictionary<string, Krile.Kernel.Control.ConfigPage> GetConfigPages()
        {
            var d = new Dictionary<string, Krile.Kernel.Control.ConfigPage>();
            d.Add("一般", new Dialogs.HConfig());
            return d;
        }

        public Krile.Kernel.Data.Service HostingService
        {
            get { return hService; }
        }

        public string InputMethodId
        {
            get { return Define.InputMethodId; }
        }

        public bool IsMasterId(string userid)
        {
            return userid == Core.Config.Account.Id;
        }

        public Krile.Kernel.Plugin.Operations.AgentElements.IStatusControl Status
        {
            get { return hStatusCtl; }
        }

        internal HStatusControl HStatusControl
        {
            get { return hStatusCtl; }
        }
    }
}
